package com.cuanbobox.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.cuanbobox.bean.DataK200Bean;
import com.cuanbobox.bean.MatrixSlefCheckBean;
import com.cuanbobox.constant.MatrixConstant;
import com.cuanbobox.utils.LogUtil;
import com.google.gson.Gson;

/**
 * 矩阵广播接收器
 */
public class MatrixBroadcastReceiver extends BroadcastReceiver {
    private MatrixListener listener;

    public MatrixBroadcastReceiver(MatrixListener listener) {
        this.listener = listener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            if (intent.getAction().equals(MatrixConstant.MATRIX_ACTION)) {
                int type = intent.getIntExtra("type", -1);
                if (type == MatrixConstant.TYPE_MATRIX_INIT_CONNECT_SUCCESS) {
                    //LogUtil.add("矩阵连接成功", "TYPE_MATRIX_INIT_CONNECT_SUCCESS", "矩阵广播接收");
                    listener.initConnectSuccess();
                } else if (type == MatrixConstant.TYPE_MATRIX_INIT_CONNECT_FAIL) {
                    //LogUtil.add("矩阵连接失败", "TYPE_MATRIX_INIT_CONNECT_FAIL", "矩阵广播接收");
                    listener.initConnectFail();
                } else if (type == MatrixConstant.TYPE_MATRIX_INIT_ERROR) {
                    //LogUtil.add("矩阵连接失败", "TYPE_MATRIX_INIT_ERROR", "矩阵广播接收");
                    listener.initError();
                } else if (type == MatrixConstant.TYPE_MATRIX_RECEIVE_MSG) {
                    String strData = intent.getStringExtra("strData");
                    LogUtil.add("接收内容", strData, "矩阵广播接收_全部");
                    listener.receiveMsg(strData);
                    //过滤指令
                    if (strData.contains("<") || strData.equals("#JSON:weburl,0.")) {
                        LogUtil.add("接收内容(过滤后)", strData, "矩阵广播接收_过滤");
                        try {
                            if (strData.contains("<APPS-START:")) {
                                strData = strData.substring(strData.indexOf("<APPS-START:") + 12);
                                strData = strData.substring(0, strData.indexOf(","));
                            } else if (strData.trim().equals("<COM:1") || strData.trim().equals("<COM:0")) {

                            } else if (strData.contains("<C")) {
                                strData = strData.substring(strData.indexOf("<C") + 2);
                                strData = strData.substring(0, strData.indexOf("."));
                            }
                            listener.receiveMsgFilter(strData);
                        } catch (Exception e) {
                            /*LogUtil.addError("矩阵接收到的消息过滤时异常！数据内容：" + strData);
                            LogUtil.addError(e);*/
                        }
                    }
                } else if (type == MatrixConstant.TYPE_READ_MATRIX_INFO_MSG_SUCCESS) {
                    String strData = intent.getStringExtra("strData");
                    if (strData.contains("MVP-K200")&&strData.contains("info")) {
                        DataK200Bean bean = new Gson().fromJson(strData, DataK200Bean.class);
                        listener.readMatrixK200CInfoMsgSuccess(bean);
                    }if (strData.contains("MCH7000C")&&strData.contains("info")) {
                        DataK200Bean bean = new Gson().fromJson(strData, DataK200Bean.class);
                        listener.readMatrixK200CInfoMsgSuccess(bean);
                    } else if (strData.contains("MVP-8C")&&strData.contains("info")) {
                        MatrixSlefCheckBean bean = new Gson().fromJson(strData, MatrixSlefCheckBean.class);
                        listener.readMatrix16CInfoMsgSuccess(bean);
                    } else if (strData.contains("MVP-16C")&&strData.contains("info")) {
                        MatrixSlefCheckBean bean = new Gson().fromJson(strData, MatrixSlefCheckBean.class);
                        listener.readMatrix16CInfoMsgSuccess(bean);
                    } else if (strData.contains("16C")&&strData.contains("info")) {
                        MatrixSlefCheckBean bean = new Gson().fromJson(strData, MatrixSlefCheckBean.class);
                        listener.readMatrix16CInfoMsgSuccess(bean);
                    } else if (strData.contains("MAC-E1")&&strData.contains("info")) {
                        MatrixSlefCheckBean bean = new Gson().fromJson(strData, MatrixSlefCheckBean.class);
                        listener.readMatrix16CInfoMsgSuccess(bean);
                    }
                } else if (type == MatrixConstant.TYPE_MATRIX_RECEIVE_ERROR) {
                    //LogUtil.add("矩阵连接失败", "TYPE_MATRIX_RECEIVE_ERROR", "矩阵广播接收");
                    listener.receiveError();
                } else if (type == MatrixConstant.TYPE_MATRIX_RECONNECT) {
                    //LogUtil.add("矩阵重新连接", "TYPE_MATRIX_RECONNECT", "矩阵广播接收");
                    listener.reConnect();
                } else if (type == MatrixConstant.TYPE_MATRIX_RECONNECT_Timer) {
                    //LogUtil.add("矩阵重新连接", "TYPE_MATRIX_RECONNECT", "矩阵广播接收");
                    listener.reConnectTimer();
                } else if (type == MatrixConstant.TYPE_MATRIX_SEND_ERROR) {
                    //LogUtil.add("矩阵连接失败", "TYPE_MATRIX_SEND_ERROR", "矩阵广播接收");
                    listener.sendError();
                } else if (type == MatrixConstant.TYPE_MATRIX_CLASS_TIMER) {
                    //LogUtil.add("矩阵上下课计时", "TYPE_MATRIX_CLASS_TIMER", "矩阵广播接收");
                    listener.classTimer();
                } else if (type == MatrixConstant.TYPE_MATRIX_IP_UNCONNECT) {
                    listener.unConnectMatrixIp();
                }
            }
        } catch (Exception e) {
            LogUtil.addError(e);
        }
    }

    public interface MatrixListener {
        void initConnectSuccess();

        void initConnectFail();

        void initError();

        void receiveMsg(String msg);

        void receiveMsgFilter(String msg);

        void receiveError();

        void reConnect();

        void sendError();

        void classTimer();

        void reConnectTimer();

        void unConnectMatrixIp();

        void readMatrix16CInfoMsgSuccess(MatrixSlefCheckBean bean);

        void readMatrixK200CInfoMsgSuccess(DataK200Bean bean);
    }

}
